<template>
	<!--表格单选-->
	<el-row class="orderExpress">
  		<el-col :span="24">
  			<!--查询过滤条件-->
  			<!-- <el-form :inline="true" :model="conditionForm" ref="conditionForm" label-width="82px">
  				<el-form-item label="名称">				
					<el-input v-model="conditionForm.name"></el-input>
				</el-form-item>
				<el-button type="primary" size="small" icon="search" @click="queryTableData">查询</el-button>
			</el-form> -->
  		</el-col>
  		<el-col :span="24">
  			<!--表格数据展示-->
  			<div>
	  			<el-button type="primary" size="small" icon="plus" v-on:click="addFn">新增</el-button>
	  			<el-button type="primary" size="small" icon="edit" v-on:click="editFn">修改</el-button>
	  			<el-button type="primary" size="small" icon="delete" v-on:click="deleteFn">删除</el-button>
  			</div>
  			<div class="data-list">
				<el-table :data="tableDatalist" border style="width: 100%" highlight-current-row @current-change="handleCurrentChange">
					<el-table-column width="50">
						<template slot-scope="scope">
							<label class="radio">
								<input type="radio" value="2" name="radio" :data-index="scope.row.index">
								<span class="dot"></span>
							</label>
						</template>
					</el-table-column>
					<el-table-column v-for="column in tableColumn" :prop="column.columnName" :label="column.columnLabel" 
						v-if="column.isColumnShow" :show-overflow-tooltip="true" sortable>
					</el-table-column>
				</el-table>
				<el-pagination
			      @size-change="pageSizeChange"
			      @current-change="handleCurrentChangePage"
			      :current-page="pagination.currentPage"
			      :page-sizes="[10,20, 50, 100, 200]"
			      :page-size="pagination.pageSize"
			      layout="->,prev, pager, next, jumper, sizes,total"
			      :total="pagination.totalSize">
			    </el-pagination>
			</div>
  		</el-col>
  		<el-col :span="24">
  			<!--新增和修改 dialog-->
			<el-dialog :title="dialogTitle" v-if="dialogVisible" :visible.sync="dialogVisible">
				<el-form :model="configform" ref="configform" :rules="rules" label-width="100px">
					<m-dialog-form-item v-for="column in tableColumn" :configform="configform" :columnInfo="column"
						:uploadImgAttr="uploadDatas">
					</m-dialog-form-item>
				</el-form>
				<div slot="footer" class="dialog-footer">
					<el-button @click="closeDialog">取 消</el-button>
					<el-button type="primary" @click="saveSubmit">确 定</el-button>
				</div>
			</el-dialog>
		</el-col>
	</el-row>
</template>
<script>
	import {
		Message
	} from 'element-ui';
	import store from '../../store'
	export default {
		components:{},
		data() {
			return {
				tableDatalist:[],
				conditionForm: { //查询过滤的条件
					name:''
				},
				pagination:{
					totalSize:0,
					pageSize: 20,
					currentPage:1,
				},
				tableColumn:[ 
					{
						columnName:'id', //主键字段，修改和删除的依赖列
						columnLabel:'ID',
						isColumnShow:false, //是否在table表格里显示
						isModifyDialogEnabled:false, //是否在新增或修改对话框中显示该属性字段
					},
					{
						columnName:'shipperId',
						columnLabel:'快递公司编号',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true, //是否在新增或修改对话框中显示该属性字段
					},
					{
						columnName:'shipperCode',
						columnLabel:'快递公司代码',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					},
					{
						columnName:'shipperName',
						columnLabel:'快递公司名',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					},
					{
						columnName:'logisticCode',
						columnLabel:'快递单号',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					},
					{
						columnName:'traces',
						columnLabel:'快递路由',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					},
					{
						columnName:'isFinish',
						columnLabel:'是否完成',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					},
					{
						columnName:'requestCount',
						columnLabel:'查询总次数',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					},
					{
						columnName:'requestTime',
						columnLabel:'上次查询时间',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					},
					{
						columnName:'addTime',
						columnLabel:'创建时间',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					},
					{
						columnName:'updateTime',
						columnLabel:'更新时间',
						isColumnShow:true, //是否在table表格里显示
						isModifyDialogEnabled:true,
					}
				],
				urlPaths:{
					queryList:'orderExpress/listAll',
					delByKey:'orderExpress/delete',
					insert:'orderExpress/add',
					updateByKey:'orderExpress/update',
					importFileUrl:store.state.global.url +'/upload/image'
				},
				configform: {},
				index: '',
				dialogVisible: false,
				dialogTitle:'新增',
				rules: {},
				currentRow: null,
				uploadImgs: {},
				uploadDatas:{
					clazz:'orderExpress',
					columnName:''
				}
			}
		}, 
		computed:{
		},
    	mounted() {
    		this.initData();
		},
		methods: {
			initData(){
    			this.queryTableData();
			},
			queryTableData(){
				/*查询接口*/
				this.$httpExt().get(this.urlPaths.queryList, {
					// name:this.conditionForm.name,
					startPage:this.pagination.currentPage,
					pageSize:this.pagination.pageSize
				}).then((response) => {
					var rows= response.rows;
					console.log(rows);
					for(let [index, item] of rows.entries()){
						item.index = index
					}
					this.tableDatalist = rows;
					this.pagination.totalSize = response.totalSize;
				},(response) => {
					Message({
						message: '服务器请求失败，请重试！',
						type: 'warning'
					});
				});
			},
			/*翻页*/
			handleCurrentChangePage(val){
				this.pagination.currentPage = val;
				// console.log(this.pagination.currentPage);
				// 数据量少，前端分页处理，如果后端分页，后端添加分页逻辑并重新调用查询接口
				this.queryTableData();
			},
			/*切换显示条数*/
			pageSizeChange(val){
				this.pagination.pageSize = val;
				// console.log(this.pagination.pageSize);
				// 数据量少，前端分页处理，如果后端分页，后端添加分页逻辑并重新调用查询接口
				this.queryTableData();
			},
			/*选择一条数据*/
			handleCurrentChange(val) {
				if(!val) {
					return
				}
				this.currentRow = val;
				this.index = val.index+''
				console.log('this.currentRow ',this.currentRow);
				console.log('this.index ',this.index);
			},
			/*新增*/
			addFn(){
				this.dialogVisible = true;
				this.dialogTitle='新增';
				/*if(this.$refs['configform']!=undefined) {
					this.$refs['configform'].resetFields();
				}*/
				this.setFormData();
			},
			/*编辑*/
			editFn(){
				if(this.index) {
					this.dialogVisible = true;
					this.dialogTitle='编辑';
					this.setFormData();
				}else{
					Message({
						message: '请选择一条进行操作！',
						type: 'warning'
					});
				}
			},
			setFormData(){
				var form ={};
				for(let col of this.tableColumn){
					if (this.dialogTitle=='新增'){
						if (col.defaultValue != undefined && col.defaultValue!=''){
							form[col.columnName] = col.defaultValue;
						} else {
							form[col.columnName] = '';
						}
					} else {
						form[col.columnName] = this.currentRow[col.columnName];
					}
				}
				this.configform = form;
			},
			/*删除*/
			deleteFn(){
				if(this.index) {
					this.$confirm('你确定要删除吗？', '删除提示', {
			          	confirmButtonText: '确定',
			          	cancelButtonText: '取消',
			          	type: 'warning'
			        }).then(() => {
			        	//调删除接口
			        	this.$httpExt().get(this.urlPaths.delByKey, {
							id:this.currentRow.id
						}).then((response) => {
//							Message({
//								message: response.msg,
//								type: 'success'
//							});
							this.queryTableData();
						},(response) => {
							Message({
								message: '服务器请求失败，请重试！',
								type: 'warning'
							});
						});
			        });
				}else{
					Message({
						message: '请选择一条进行操作！',
						type: 'warning'
					});
				}
			},
			/*提交*/
			saveSubmit(){				
				var that = this;
				if(this.dialogTitle == '新增') {
//							that.configform.id = '';
					var url = this.urlPaths.insert;
				} else {
					var url = this.urlPaths.updateByKey;
				}
				console.log(url);
				console.log(that.configform);
				this.$httpExt().post(url,that.configform).then((response) => {
					if(response.succ == 'ok') {
						that.queryTableData();
						that.dialogVisible = false;
					}
				}, (data) => {
					Message({
						message: data.msg ? data.msg : '服务器请求失败，请重试！',
						type: 'warning'
					});
				});
			},
			closeDialog(){
				this.dialogVisible = false;
			},
			handleReset() {
				this.newlyFormVisible = false
			},
	    }
	}
</script>
<style>
.fl{
	float: left;
}
.orderExpress a{
	color: #20a0ff;
	text-decoration: none;
}

</style>